/**
 * 
 */
package com.jm.report.datasource;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.jm.utils.StringHelper;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;

/**
 * @author yanjie
 * Dec 2, 2011 9:42:28 AM
 */
public class ResultSetJRDataSource implements JRDataSource{
	private ResultSet rs = null;
	
	public ResultSetJRDataSource(ResultSet rs) {
		this.rs = rs;
	}
	
	public boolean next() throws JRException {
		if(this.rs == null)
			return false;
		
		try {
			return this.rs.next();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public Object getFieldValue(JRField field) throws JRException {
		if(this.rs == null)
			return "";
		
		try
		{
			Object object = rs.getObject(field.getName());
			if(object instanceof String)
				object = StringHelper.getValue(object,"").trim();
			return object;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return "";
		}
	}

}
